package uit.com.project.iCampus.xmlparser;

import java.util.ArrayList;
import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import android.util.Log;

import uit.com.project.iCampus.model.BuildingInfo;
import uit.com.project.iCampus.model.BuildingTypeEnum;

public class BuildingInfoXMLHandler extends DefaultHandler {
	Boolean currentElement = false;
	String currentValue = null;
	private List<BuildingInfo> buildingInfos = new ArrayList<BuildingInfo>();
	private BuildingInfo buildingInfo = null;

	/**
	 * @return the buildingInfos
	 */
	public List<BuildingInfo> getBuildingInfos() {
		return buildingInfos;
	}

	/**
	 * @param buildingInfos the buildingInfos to set
	 */
	public void setBuildingInfos(List<BuildingInfo> buildingInfos) {
		this.buildingInfos = buildingInfos;
	}

	/**
	 * @return the buildingInfo
	 */
	public BuildingInfo getBuildingInfo() {
		return buildingInfo;
	}

	/**
	 * @param buildingInfo the buildingInfo to set
	 */
	public void setBuildingInfo(BuildingInfo buildingInfo) {
		this.buildingInfo = buildingInfo;
	}

	/**
	 * Called when tag starts ( ex:- <name>AndroidPeople</name> -- <name> )
	 */
	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		currentElement = true;
		String element = (qName.equals("") ? localName : qName);
		if (element.equals(XMLParserAndroidUtil.ELEMENT)){
			if (attributes.getValue(0).equals("buildingInfo")){
				buildingInfo = new BuildingInfo();
				buildingInfos.add(buildingInfo);
			}
		} else if (element.equals("buildingInfo")) {
			buildingInfo = new BuildingInfo();
			buildingInfos.add(buildingInfo);
		} 
	}

	
	/**
	 * Called when tag closing ( ex:- <name>AndroidPeople</name> -- </name> )
	 */
	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		currentElement = false;
		String element = (qName.equals("") ? localName : qName);
		/** set value */
		if (element.equalsIgnoreCase("buidingTypeEnum")){
			Log.i("currentValue Enum", currentValue);
			if (currentValue.equals("TDRINK")) {
				currentValue = "SOFTDRINK";
			}
			buildingInfo.setBuidingTypeEnum(BuildingTypeEnum.valueOf(currentValue));
		}
		else if (element.equalsIgnoreCase("buildingName"))
			buildingInfo.setBuildingName(currentValue);
		else if (element.equalsIgnoreCase("detail"))
			buildingInfo.setDetail(currentValue);
		else if (element.equalsIgnoreCase("idBuilding"))
			buildingInfo.setIdBuilding(Long.parseLong(currentValue));
		else if (element.equalsIgnoreCase("latitude"))
			buildingInfo.setLatitude(Float.parseFloat(currentValue));
		else if (element.equalsIgnoreCase("longitude"))
			buildingInfo.setLongitude(Float.parseFloat(currentValue));
		else if (element.equalsIgnoreCase("rateDouble"))
			buildingInfo.setRateDouble(Double.parseDouble(currentValue));
	}

	/**
	 * Called to get tag characters ( ex:- <name>AndroidPeople</name> -- to get
	 * AndroidPeople Character )
	 */
	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		String chars = (new String(ch).substring(start, start + length));
		if (currentElement) {
			currentValue = chars;
		}
	}
}
